<!-- 销售管理->样品订单 -->
<template>
    <div class="content-wrapper a-base-container">
        <div class="requisition-set-wrapper">
            <!-- 按钮 -->
            <div class="btn-wrapper clearfix">
                <btns-wrapper @searchClick="searchClick" :showModel="showModel" :orderId="orderId" :voucherState="voucherState" :editMask="editMask" @printOrder="printOrder" @switchList="switchList" @submitServer="submitServer" @clickEdit="clickEdit" @deleteOrder="deleteOrder" @getIdByData="getIdByData" @auditClick="auditClick" @turnThePage="turnThePage" @syncOrder="syncOrder" @submitOrder="submitOrder" @exportDetail="exportDetail" :sourceCode="sourceCode">
                    <template slot="neworder">
                        <a-dropdown class="btns" key="new" style="display:block">
                            <a class="ant-dropdown-link" @click="e => e.preventDefault()">
                                <a-icon class="btns-icon" type="file-add" />
                                <div class="btns-text">新增
                                    <a-icon type="down" style="color:rgb(24,144,255);vertical-align:middle;" />
                                </div>
                            </a>
                            <a-menu slot="overlay">
                                <a-menu-item key="0">
                                    <a href="javascript:void(0)" @click="newForecast">新增</a>
                                </a-menu-item>
                                <!-- <a-menu-item key="1">
                                    <a href="javascript:void(0)" @click="choiceBillsFromSaleQuotation">选报价单</a>
                                </a-menu-item> -->
                            </a-menu>
                        </a-dropdown>
                    </template>
                    <template slot="shengdan">
                        <a-dropdown class="btns" key="sheng" style="display:block">
                            <a class="ant-dropdown-link" @click="e => e.preventDefault()">
                                <a-icon class="btns-icon" type="snippets" />
                                <div class="btns-text">生单
                                    <a-icon type="down" />
                                </div>
                            </a>
                            <a-menu v-if="voucherState==1" slot="overlay">
                                <a-menu-item key="0">
                                    <a href="javascript:void(0)" @click="generateBills('Contract')">生成合同</a>
                                </a-menu-item>
                            </a-menu>
                        </a-dropdown>
                    </template>
                </btns-wrapper>
            </div>
            <div ref="print" class="documents-wrapper" id="documents-wrapper">
                <h3 class="text-center">
                    {{voucherTitle}}
                </h3>
                <section v-show="showModel=='list'">
                    <!--列表-->
                    <dynamicList ref="dyList" :basePostData="basePostData" :sourceCode="sourceCode" @switchList="switchList" @dblclick="getIdByData" :reportCode="reportCodeList"></dynamicList>
                </section>
                <section v-show="showModel=='order'">
                    <div><span class="audit-status" v-show="voucherState==0">待审核</span><span class="audit-status" v-show="voucherState==1 || voucherState==10">{{voucherState==1?'已审核':'关闭'}}</span></div>
                    <a-form-model :layout="formLayout">
                        <div class="header-box">
                            <order-dynamic-form :FormData="orderHeaderData" @changetime="onChangeTime" @clickItem="setValue"></order-dynamic-form>
                            <div class="edit-mask-header" v-show="editMask">
                            </div>
                        </div>
                        <div class="table-box">
                            <a-table id="bills_tb" :columns="columns" :data-source="dataSource" :customRow="customClick" :scroll="{ x: 1000, y: 347 }" :pagination="false" @change="onTableChange" :size="small" bordered>
                                <template v-if="isRouterAlive" slot="number" slot-scope="text, record, index">
                                    <div v-if="index == dataSource.length-1">
                                        合计
                                    </div>
                                    <div v-else>
                                        {{index+1}}
                                    </div>
                                </template>
                                <template v-for="col in slotArray" :slot="col" slot-scope="text, record,index">
                                    <div data-key="">
                                        <editable-cell v-if="isRouterAlive" :checkType="checkType" :text="text" :col="col" :index="index" :record="record" :dataSource="dataSource" :unitsDataDetails="unitsDataDetails" :unitsAssistSelect="unitsAssistSelect" :dynamicAllData="dynamicFormData" :dynamicFormDataMap="dynamicFormDataMap"  @choiceAddData="choiceAddData(index,col,$event)" @change="onCellChange(index, col, $event)" @newAddData="handleAdd" />
                                    </div>
                                </template>
                            </a-table>
                            <div class="edit-mask" v-show="editMask">
                            </div>
                        </div>
                        <div class="footer-box">
                            <order-dynamic-form :FormData="orderFooterData"  @changetime="onChangeTime" @clickItem="setValue"></order-dynamic-form>
                            <div class="edit-mask-footer" v-show="editMask">
                            </div>
                        </div>
                    </a-form-model>
                </section>
                <section v-show="showModel=='detail'">
                    <!--明细-->
                    <dynamicListDetails ref="detailSearchRef" :reportCode="reportCodeDetail" :componentType="1" :basePostData="basePostData"></dynamicListDetails>
                </section>
                <section v-show="showModel=='statistical'">
                    <!--统计-->
                    <dynamicListDetails ref="statisticalSearchRef" :reportCode="reportCodeStatistical" :componentType="2" :basePostData="basePostData"></dynamicListDetails>
                </section>
            </div>
            <div class="contextmenu-box">
                <!-- 右键菜单 -->
                <ul v-show="visible" :style="{left:left+'px',top:top+'px'}" class="contextmenu">
                    <li @click="handleAdd()">新增行</li>
                    <li @click="handleDelete()">删除行</li>
                    <!-- <li @click="handleCopy()">复制行</li> -->
                </ul>
            </div>
            <drawerBox ref="searchList" :unitData="unitData" :unitsData="unitsData" :unitsDataDetails="unitsDataDetails" @editClickOrder="editClickOrder"></drawerBox>
        </div>

        <!--加载中-->
        <div class="loading-wrapper" v-show="loading">
            <a-spin class="loading-content" tip="加载中..." />
        </div>
        <!--用做横向打印-->
        <!-- <div v-html="stylePrint"></div> -->
        <print-modal ref="printDom" v-if="printVisible" :incomingPrintData="incomingPrintData" @cancelPrint="cancelPrint"></print-modal>

        <!--销售订单  分单 销售发货单-->
        <sale-in-modal ref="saleRef"></sale-in-modal>
    </div>
</template>
<script>


import { save, examine, remove, getCode } from '@/api/sale/order'


import pro from '@/mixins/procurement/pro';
import saleInModal from '@/components/SeparateBill/saleInModal.vue';

export default {
    components: { saleInModal },
    mixins: [pro],
    data () {
        return {
            //基础 查询条件（列表 + 翻页）
            basePostData: {
                dataSource: "SampleOrder",
            },
            sourceCode: 'SampleOrder',                              //查询字段设计 和 保存数据来源 使用
            reportCodeDetail: 'SampleOrder_Detail_Report',          //明细code
            reportCodeStatistical: 'SampleOrder_Statistic_Report',   //统计code
            reportCodeList: 'SampleOrder_List_Report',              //列表code

            checkType: 'is_for_sale',//销售
            financeType: true,
        };
    },
    created () {
        // this.getIdByData();//获取最新一条数据
    },
    methods: {
        //新增预测单
        newForecast () {
            this.showModel = 'order';
            this.editMask = false;
            this.loading = false;
            this.voucherState = -1;
            this.sourceVoucherData = {};//清空来源数据
            this.initForm(1);
        },
        //保存
        submit (requestData) {
            save(requestData).then(response => {
                if (response.code === 200) {
                    this.$message.success('保存成功');
                    this.editMask = true;
                    this.isSaveRun = true;
                    if (!this.orderId) {
                        this.getIdByData(); //获取最新一条数据
                    }
                } else {
                    this.$message.error(response.message);
                    this.isSaveRun = true;
                }
            }).catch((e) => {
                this.isSaveRun = true;
            })
        },
        //审核
        auditClick (type) {
            let requestData = {
                id: this.orderId,
                status: type
            }
            examine(requestData).then((response) => {
                if (response.code === 200) {
                    const backData = response.data;
                    if (type == 1) {
                        this.$message.success('审核成功');
                        this.getIdByData();
                    }
                    if (type == 0) {
                        this.$message.success('弃审成功');
                        this.getIdByData();
                    }
                } else {
                    // this.$message.warning(response.message);
                }
            })
        },
        //删除
        remove (postData) {
            remove(postData).then(response => {
                if (response.code === 200) {
                    this.$message.success('删除成功');
                    this.orderId = '';
                    this.voucherState = 0;
                    this.getIdByData();//获取最新一条数据
                } else {
                    this.$message.error(response.message);
                }
            })
        },
        //获取单据编号
        getCode () {
            const { sourceCode } = this;
            getCode({ dataSource: sourceCode }).then((res) => {
                if (res.code == 200) {
                    this.receiptNumber = res.message;
                    for (let i = 0; i < this.orderHeaderData.length; i++) {
                        if (this.orderHeaderData[i].code == 'voucherCode') {
                            this.$set(this.orderHeaderData[i], 'onEdit', false)
                            this.$set(this.orderHeaderData[i], 'value', this.receiptNumber)
                        }
                    }
                } else {
                    // this.$message.warning(res.message);
                }
            });
        },
        // 选择 报价单
        choiceBillsFromSaleQuotation () {
            let postData = {
                sourceCode: 'SaleQuotation',      // 来源单
                title: '选报价单',
                businessType: 2, //1:退货业务 2：非退货业务 3: 退货 + 非退货 4: 发票
                someBusinessType: '0',    // 指定业务类型 默认 0
                startVoucherState: 0,
                endVoucherState: 10
            }
            this.$refs.searchList.onOpenBox(postData);
        }
    }
};
</script>
<style lang="less">
    @import '~@/style/purchase/voucher.less';

    .conten-button {
        display: flex;
        width: 50%;
        div {
            width: 30%;
        }
    }

    // 记录/ 研发
    .conten-jl {
        border: 1px solid #cacaca;
        border-bottom: 0;
        border-top: 0;
        display: flex;
        .conten-jl-one {
            border: 1px solid #cacaca;
            border-bottom: 0;
            border-top: 0;
            width: 50%;
        }
        .conten-jl-tow {
            border: 1px solid #cacaca;
            border-bottom: 0;
            border-top: 0;
            width: 50%;
        }
    }
    .conten_input {
        div {
            border-bottom: 0;
        }
    }
    // 备注
    .conten-bz {
        height: 100px;
        width: 50%;
        display: flex;
        border: 1px solid #cacaca;
        border-bottom: 0;
        .conten-bz-one {
            width: 8%;
            border: 1px solid #cacaca;
            text-align: center;
            // border-bottom: 0;
            height: 100px;
            line-height: 100px;
        }
        .conten-bz-tow {
            height: 100px;
            width: 85%;
            border: 1px solid #cacaca;
        }
    }
    // 复选
    .conten-chec {
        display: flex;
        flex-wrap: wrap;
        width: 50%;
        border: 1px solid #cacaca;
        border-bottom: 0;
        span {
            padding: 0 10px 0 5px;
        }
        input {
            position: relative;
            top: 2px;
        }
        div {
            padding: 0 0 0 15px;
            height: 30px;
            line-height: 30px;
        }
    }
    // 订单
    .conten-order {
        display: flex;
        width: 50%;
        .conten-order-one {
            width: 65%;
        }
        .conten-order-tow {
            width: 14%;
        }
    }

    .content-app-wrapper {
        height: 100%;
        overflow-y: auto;
        padding: 10px;
    }
    .conten-top {
        display: flex;
        width: 50%;
        height: 100px;
        line-height: 100px;
    }
    .conten-title {
        display: flex;
        div {
            // border: 1px solid #cacaca;
            // border-bottom: 0;
            // border-top: 0;
            border-left: 1px solid #cacaca;
            width: 19%;
            overflow: hidden;
        }
    }
    .conten-tz {
        width: 50%;
        border: 1px solid #cacaca;
        height: 30px;
        line-height: 30px;
        text-align: center;
        overflow: hidden;
    }
    .conten-img {
        width: 14%;
    }
    .conten-txt {
        height: 25px;
        line-height: 25px;
    }
    .conten-input input {
        border: 0;
    }
    .conten-input {
        font-size: 13px;
    }

    .conten-input input:hover {
        border: 0;
    }
</style>
